package com.jxnu.duewang.zmly.zmly.fragment;


import android.content.Intent;
import android.os.Bundle;
import com.google.android.material.tabs.TabLayout;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

import com.jxnu.duewang.zmly.R;
import com.jxnu.duewang.zmly.zouZhen.activity.SearchActivity;
import com.jxnu.duewang.zmly.zouZhen.adapter.CommonFragmentAdapter;
import com.jxnu.duewang.zmly.zouZhen.agency.DiscoveryService;
import com.jxnu.duewang.zmly.fragment.AnchorFragment;
import com.jxnu.duewang.zmly.fragment.BaseFragment;
import com.jxnu.duewang.zmly.fragment.BroadcastFragment;
import com.jxnu.duewang.zmly.fragment.ClassifyFragment;
import com.jxnu.duewang.zmly.fragment.ListFragment;
import com.jxnu.duewang.zmly.fragment.RecommendFragment;
import com.jxnu.duewang.zmly.zouZhen.model.DiscoveryTiTleDataThree;
import com.jxnu.duewang.zmly.zouZhen.model.DiscoveryTitleData;
import com.jxnu.duewang.zmly.zouZhen.model.DiscovetyTiTleDataTwo;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * A simple {@link Fragment} subclass.
 */
public class DiscoverFragment extends Fragment {

    private ArrayList<BaseFragment> mFragments=null;
    private CommonFragmentAdapter mAdapter;

    public DiscoverFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View ret = inflater.inflate(R.layout.fragment_discover, container, false);
        Toolbar toolbar = (Toolbar) ret.findViewById(R.id.discover_toolbar);
        toolbar.setLogo(R.mipmap.title_logo);
        toolbar.setTitle("");
        ((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
        setHasOptionsMenu(true);

        fragmentTitle();

        ViewPager pager = (ViewPager) ret.findViewById(R.id.discover_viewpager);
        pager.setOffscreenPageLimit(4);
        TabLayout tabLayout = (TabLayout) ret.findViewById(R.id.discover_tabs);
        ArrayList<BaseFragment> fragments = new ArrayList<>();
        if (pager != null) {
           if(mFragments==null||mFragments.isEmpty()){
               fragments.add(new RecommendFragment());
               fragments.add(new ClassifyFragment());
               fragments.add(new BroadcastFragment());
               fragments.add(new ListFragment());
               fragments.add(new AnchorFragment());
           }else{
               fragments=mFragments;
           }
            mAdapter = new CommonFragmentAdapter(getChildFragmentManager(), fragments);
            pager.setAdapter(mAdapter);
            tabLayout.setupWithViewPager(pager);
        }

        return ret;
    }

    public DiscoveryTitleData fragmentTitle(){
        DiscoveryService discoveryService = new Retrofit.Builder()
                .baseUrl("http://mobile.ximalaya.com/")
                .addConverterFactory(GsonConverterFactory.create())
                .build()
                .create(DiscoveryService.class);
        Call<DiscoveryTitleData> discoveryTitleData = discoveryService.getDiscoveryTitleData();
        discoveryTitleData.enqueue(new Callback<DiscoveryTitleData>() {
            @Override
            public void onResponse(Call<DiscoveryTitleData> call, Response<DiscoveryTitleData> response) {
                if (response.isSuccessful()) {
                    DiscoveryTitleData body = response.body();
                    DiscovetyTiTleDataTwo discovetyTiTleDataTwo = body.getDiscovetyTiTleDataTwo();
                    int count = discovetyTiTleDataTwo.getCount();
                    List<DiscoveryTiTleDataThree> discoveryTiTleDataThrees = discovetyTiTleDataTwo.getDiscoveryTiTleDataThrees();
                    mFragments = new ArrayList<>();
                    for (int i = 0; i < count; i++) {
                        DiscoveryTiTleDataThree discoveryTiTleDataThree = discoveryTiTleDataThrees.get(i);
                        switch (discoveryTiTleDataThree.getTitle()) {
                            case "推荐":
                                mFragments.add(new RecommendFragment());
                                break;
                            case "分类":
                                mFragments.add(new ClassifyFragment());
                                break;
                            case "广播":
                                mFragments.add(new BroadcastFragment());
                                break;
                            case "榜单":
                                mFragments.add(new ListFragment());
                                break;
                            case "主播":
                                mFragments.add(new AnchorFragment());
                                break;
                        }
                    }
                    mAdapter.notifyDataSetChanged();
                }
            }

            @Override
            public void onFailure(Call<DiscoveryTitleData> call, Throwable t) {

            }
        });
        return null;
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        menu.clear();
        inflater.inflate(R.menu.discover_menu,menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_search:
                startActivity(new Intent(getContext(), SearchActivity.class));
                getActivity().overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_in_left);
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

}
